﻿using MediatR;
using TraceabilityApp_Api.Application.Command.DeepProcessing_深加工_;
using TraceabilityApp_Api.DTO;
using TraceabilityApp_Domain;
using TraceabilityApp_ErrorCode;
using TraceabilityApp_Infrastructure;

namespace TraceabilityApp_Api.Application.Handler.DeepProcessing_深加工_
{
    public class GetSupplierHandler : IRequestHandler<GetSupplierCommand, ApiResult<List<SupplierModel>>>
    {
        private readonly IBase<SupplierModel> supplierRepo;

        public GetSupplierHandler(IBase<SupplierModel> supplierRepo)
        {
            this.supplierRepo = supplierRepo;
        }
        public Task<ApiResult<List<SupplierModel>>> Handle(GetSupplierCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<SupplierModel>> result = new ApiResult<List<SupplierModel>> ();
            var supplier = supplierRepo.GetAll();
            if(!string.IsNullOrEmpty(request.Batch))
            {
                supplier = supplier.Where(x=>x.Batch.Contains(request.Batch));
            }
            if(request.SupplierType!=null)
            {
                supplier = supplier.Where(x => x.SupplierType==request.SupplierType);
            }
            result.Data = supplier.ToList();
            result.Code = ApiEnum.OK;
            result.Msg = "显示成功";
            return Task.FromResult (result);
        }
    }
}
